package com.woniuxy.enjoylife.controller;



import com.github.pagehelper.PageInfo;
import com.woniuxy.enjoylife.entity.RecentComment;
import com.woniuxy.enjoylife.entity.dto.*;
import com.woniuxy.enjoylife.service.RecentCommentService;
import com.woniuxy.enjoylife.utils.PageResult;
import com.woniuxy.enjoylife.utils.ResponseResult;
import com.woniuxy.enjoylife.utils.TableResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/recentcomment")
public class RecentCommentController {

    @Resource
    private RecentCommentService recentCommentServiceImpl;

    @Resource
    private HttpServletRequest request;

    //新增
    @RequestMapping("/addcomment")
    public ResponseResult addComment(RecentComment recentComment) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.success();
        recentComment.setTime(LocalDateTime.now());
        int userid=(int) request.getSession().getAttribute("id");
        recentComment.setUid(userid);

        recentCommentServiceImpl.addComment(recentComment,userid);
        return responseResult;
    }

    //修改
    public ResponseResult updateComment(RecentComment recentComment){
        ResponseResult responseResult = new ResponseResult();
        responseResult.success();
        recentCommentServiceImpl.updateComment(recentComment);
        return responseResult;
    }

    //删除
    @RequestMapping("/deletecomment")
    public ResponseResult deleteComment(int id) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.success();
        recentCommentServiceImpl.deleteComment(id);
        return responseResult;
    }

    //查询某人的评论
    @RequestMapping("/pagecomment")
    public PageResult<RecentDto> selectComment(int page,int size){
        int uid = (int) request.getSession().getAttribute("id");
        return recentCommentServiceImpl.selectComment(page,size,uid);
    }

    //查询某动态的评论
    @RequestMapping("/singleComment")
    public List<RecentCommentDto2> selectSingleComment(int rid){
//        RecentCommentDto2 recentCommentDto2 = new RecentCommentDto2();
//        recentCommentDto2.getRid();
        return recentCommentServiceImpl.selectSingleComment(rid);
    }

    /**
     * 查询所有评论
     * @param content
     * @param current
     * @param size
     * @return
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public ResponseResult<PageResult> findAll(String content,int current, int size) {
        System.out.println("findUserSuggest"+current+":"+size+":"+content);
        ResponseResult responseResult = new ResponseResult();
        PageResult<List<RecentDetailDto>> pageResult = recentCommentServiceImpl.findAll(content,current,size);
        if (pageResult == null) {
            responseResult.error();
        } else {
            responseResult.success();
            responseResult.setData(pageResult);
        }
        return responseResult;
    }

    /**
     * 恢复某条已经删除的评论
     * @param id
     * @return
     */
    @RequestMapping("/restorecomment")
    @ResponseBody
    public ResponseResult restoreComment(int id){
        ResponseResult responseResult = new ResponseResult();
        if(recentCommentServiceImpl.restorecomment(id)){
            responseResult.success();
        }else{
            responseResult.error();
        }

        return responseResult;
    }

    /**
     * 模糊插叙评论
     * @param content
     * @return
     */
    @RequestMapping("/findByLike")
    @ResponseBody
    public ResponseResult<PageResult> findByLike(String content,int current, int size){
        System.out.println(content+current+size);
        ResponseResult responseResult = new ResponseResult();
        PageResult<List<RecentDetailDto>> pageResult = recentCommentServiceImpl.findByLike(content,current,size);
        if(pageResult == null){
            responseResult.error();
        }else{
            responseResult.success();
            responseResult.setData(pageResult);
        }
        return responseResult;
    }


    /**
     * 查询动态评论及其评论人信息
     * @return
     */
    @RequestMapping("/findComments")
    @ResponseBody
    public ResponseResult findComments(int id){
        ResponseResult responseResult = new ResponseResult();
        List<RecentCommentDto> recentCommentDtos= recentCommentServiceImpl.findRecentDetail(id);
        if(recentCommentDtos.size()>0){
            responseResult.success();
            responseResult.setData(recentCommentDtos);
        }else {
            responseResult.error();
        }
        return responseResult;
    }

}
